<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .router-link-active{
      background: red;
    }
  </style>
</head>
<body>
  <div id="app">
   
    <router-link tag="button" to="/news/2">
      新闻2
    </router-link>
    <router-link tag="button" to="/news/3">
      新闻3
    </router-link>
    <router-link tag="button" to="/news/4">
      新闻4
    </router-link>
    <router-link tag="button" to="/news/5">
      新闻5
    </router-link>
    
    <router-view></router-view>
  </div>
  <script src="vue.js"></script>
  <script src="vue-router.js"></script>
  <script>
 
    const Home = {
      template: `
        <div>
          <h1> home页 </h1>
        </div>
      `
    }
    const News = {
      props:['id'],
      template: `
        <div>
          <h1> 新闻页 </h1>
          {{ $route.params.id }}
          {{ id }}
        </div>
      `,
      created(){
      
       this.fetchData()
      },
      methods:{
        fetchData(){
          // 发送ajax请求，把 this.$route.params.id传给后台接口
          console.log(this.$route.params.id);
        }
      },
      watch: {
        $route(){  // 监听 路由参数的变化
          console.log(1111)
          this.fetchData()
        }
      },
    }
    const About = {
      template: `
        <div>
          <h1> 关于我们页 </h1>
        </div>
      `
    }
    // 以上称为 路由组件 需要在 路由 配置中 挂载
    // 定义路由
    const routes = [ // 路由规则
     /*  {
        path: '/',
        component: Home
      }, */
      {
        path: '/',
        redirect: '/home'
      },
      {
        path: '/home',
        component: Home
      },
      {
        path: '/news/:id',
        component: News,
        props: true
      },
      {
        path: '/about',
        component: About
      }
    ]
    const router = new VueRouter({
      routes
    })
    const vm = new Vue({
      el: '#app',
      router
    })
  </script>
</body>
</html>